package flc.ast.activity;

import android.net.Uri;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hpplay.sdk.source.api.LelinkPlayerInfo;
import com.hpplay.sdk.source.api.LelinkSourceSDK;
import com.stark.cast.screen.CastScreenManager;
import com.stark.picselect.entity.SelectMediaEntity;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.ProPicAdapter;
import flc.ast.databinding.ActivityProPicBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.utils.UriUtil;

public class ProPicActivity extends BaseAc<ActivityProPicBinding> {

    public static List<SelectMediaEntity> listPro = new ArrayList<>();
    private ProPicAdapter proPicAdapter;
    private CastScreenManager mCastScreenManager;
    private int oldPosition;

    @Override
    protected int onCreate() {
        return R.layout.activity_pro_pic;
    }

    @Override
    protected void initView() {
        mDataBinding.ivProPicBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        mDataBinding.rvProPicList.setLayoutManager(layoutManager);
        proPicAdapter = new ProPicAdapter();
        for (SelectMediaEntity bean : listPro) {
            bean.setChecked(false);
        }
        listPro.get(0).setChecked(true);
        Glide.with(mContext).load(listPro.get(0).getPath()).into(mDataBinding.ivProImgSel);
        proPicAdapter.setList(listPro);
        mDataBinding.rvProPicList.setAdapter(proPicAdapter);
        proPicAdapter.setOnItemClickListener(this);
        mCastScreenManager = CastScreenManager.getInstance();
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        proPicAdapter.getItem(oldPosition).setChecked(false);
        oldPosition = position;
        proPicAdapter.getItem(position).setChecked(true);
        proPicAdapter.notifyDataSetChanged();
        Glide.with(mContext).load(listPro.get(position).getPath()).into(mDataBinding.ivProImgSel);
        proPicture(proPicAdapter.getItem(position).getPath());
    }

    @Override
    protected void initData() {
        proPicture(listPro.get(oldPosition).getPath());
        saveRecord();
    }

    //图片投屏
    private void proPicture(String url) {
        LelinkPlayerInfo playerInfo = new LelinkPlayerInfo();
        playerInfo.setType(LelinkSourceSDK.MEDIA_TYPE_IMAGE);
        Uri uri = Uri.parse(url);
        if (UriUtil.isContentSchemeUri(uri)) {
            playerInfo.setLocalUri(uri);
        } else {
            playerInfo.setLocalPath(url);
        }
        mCastScreenManager.startPlay(playerInfo);
    }

    private void saveRecord() {
        List<String> listSave = new ArrayList<>();
        for (SelectMediaEntity selectMediaEntity : listPro) {
            listSave.add(selectMediaEntity.getPath());
        }
        List<String> listSp = AppSpUtils.getPicList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            AppSpUtils.savePicList(listSp);
        } else {
            AppSpUtils.savePicList(listSave);
        }
    }

}